package com.juvepoland.jpdroid.activities;

import com.juvepoland.jpdroid.R;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class NewsActivity extends TabActivity {
	
	protected TabHost tabHost;
	protected SharedPreferences sharedPreferences;
	protected SharedPreferences.Editor sharedPreferenceEditor;
	protected static final String MY_PREFERENCES = MyPreferencesActivity.MY_PREFERENCES;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_activity_main_layout);
		tabHost = getTabHost();
		sharedPreferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
		sharedPreferenceEditor = sharedPreferences.edit();
		
		addTabsToTabhost();
		
		changeTabStyles();
		
		addTabChangedListener();
		
	}

	protected void addTabChangedListener() {
		
		tabHost.setOnTabChangedListener(new OnTabChangeListener(){
			@Override
			public void onTabChanged(String tabId) {
			     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
			        {
			           tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#BFBFBF")); //unselected
			        }
			        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#878787")); // selected
			}
			});
		
	}

	private void changeTabStyles() {
	
		for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
	    {
		    if (i == 0) tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#878787"));
	
		    else tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#BFBFBF"));
		    
		    RelativeLayout rllf = (RelativeLayout)tabHost.getTabWidget().getChildAt(i);
		    TextView lf = (TextView) rllf.getChildAt(1);
		    
		    String fontPath = sharedPreferences.getString(MyPreferencesActivity.PREFERENCE_DEFAULT_FONT_NAME, null);
	        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
	        lf.setTypeface(tf);
	    }  
		
	}

	protected void addTabsToTabhost() {
		
		TabSpec eventsSpec = tabHost.newTabSpec("Wydarzenia");
		eventsSpec.setIndicator("Wydarzenia", getResources().getDrawable(R.drawable.icon_events_tab));
		Intent eventsIntent = new Intent(this, EventsActivity.class);
		eventsSpec.setContent(eventsIntent);
		
		TabSpec otherSpec = tabHost.newTabSpec("Pozostałe");
		otherSpec.setIndicator("Pozostałe", getResources().getDrawable(R.drawable.icon_other_news_tab));
		Intent otherNewsIntent = new Intent(this, OtherNewsActivity.class);
		otherSpec.setContent(otherNewsIntent);
		
		
		
		tabHost.addTab(eventsSpec);
		tabHost.addTab(otherSpec);
		
	}

	

}

